﻿using System;
using System.Collections.Generic;
using System.Configuration;
using Web4Comment.SNSAPI.sina;
using UnlessComment;
using System.Web;

namespace Web4Comment.Service
{
    public class SNSService:ISNSService
    {
        static string clientID = ConfigurationSettings.AppSettings["sina_clientID"];//app_key

        static string responseType = "code";
        static string redirectUri = ConfigurationSettings.AppSettings["redirectUri"];//回调地址
        static string clientSecret = ConfigurationSettings.AppSettings["sina_app_secret"];//app_secret'

        static SinaSerive sina = new SinaSerive(clientID, clientSecret, redirectUri);

        public int authorize(int SNSWeb)
        {
            //链接、授权
            SNSAPI.ISnsSerive serive = this.getSnsService(SNSWeb);

            //已绑定
            if (this.isSaveUserSite(SNSWeb, (int)HttpContext.Current.Session["user_id"]) != null)
                return 0;
            
            serive.GetAuthorizationCode();
            
            return 1;
        }

        public int connect(int snsWeb)
        {
            SNSAPI.ISnsSerive serive = this.getSnsService(snsWeb);

            //未绑定
            if (this.isSaveUserSite(snsWeb, (int)HttpContext.Current.Session["user_id"]) == null)
                return 0;

            serive.GetAuthorizationCode();
            return 1;
        }
        /// <summary>
        /// 获得sns对应的接口
        /// </summary>
        /// <param name="sns">sns id</param>
        /// <returns>对应接口</returns>
        private SNSAPI.ISnsSerive getSnsService(int sns)
        {
            
            switch (sns)
            {
                case 1:
                    return sina;
                   
            }
            return null;

        }
        public List<UnlessComment.Model.SnsFriend> getNewSNSFriends(int SNSWeb)
        {
            SNSAPI.ISnsSerive serive = this.getSnsService(SNSWeb);
            long uid = long.Parse(serive.getUidByToken());

            //friend id 列表
            List<long> friend_id_list = new List<long>();
            //取得friend列表
            SNSAPI.sina.Users users = (SNSAPI.sina.Users)serive.Friendships_Friends(uid, 100, 0);

            //加入到列表中
            foreach (SNSAPI.sina.User user in users.users){
                friend_id_list.Add(user.id);
            }

            while (users.next_cursor != 0)
            {
                users = (SNSAPI.sina.Users)serive.Friendships_Friends(uid, 100, (int)users.next_cursor);

                foreach (SNSAPI.sina.User user in users.users)
                {
                    friend_id_list.Add(user.id);
                }
            }

            List<UnlessComment.Model.SnsFriend> list = new List<UnlessComment.Model.SnsFriend>();
            UnlessComment.BLL.User userDao = new UnlessComment.BLL.User();
            
            foreach (long friend_id in friend_id_list)
            {
                UnlessComment.Model.UserSite result = this.getUserInSiteByUid(SNSWeb, friend_id);
                if (result != null)
                {
                    
                    UnlessComment.Model.SnsFriend f = new UnlessComment.Model.SnsFriend();
                    f.sns_name = serive.getSnsName();
                    f.userSite = result;
                    
                    f.user = userDao.GetModel((int)result.user_id);

                    if (!FriendsService.checkIsFriend(HttpContext.Current.Session["username"] as String, f.user.username))
                        list.Add(f);
                }
            }

            return list;
        }

        

        public void saveUserSite(int sid, int user_id)
        {
            SNSAPI.ISnsSerive serive = this.getSnsService(sid);
            string uid = serive.getUidByToken();

            UnlessComment.BLL.UserSite dao = new UnlessComment.BLL.UserSite();
            UnlessComment.Model.UserSite userSite = new UnlessComment.Model.UserSite();

            SNSAPI.sina.User user = (SNSAPI.sina.User)serive.Users_Show(long.Parse(uid));

            userSite.sns_id = sid;
            userSite.uid = uid;
            userSite.user_id = user_id;
            userSite.date = DateTime.Now;
            userSite.screen_name = user.screen_name;

            dao.Add(userSite);
        }


        public UnlessComment.Model.UserSite getUserInSiteByUid(int sid, long uid)
        {
            SNSAPI.ISnsSerive serive = this.getSnsService(sid);
            UnlessComment.BLL.UserSite dao = new UnlessComment.BLL.UserSite();
            List<UnlessComment.Model.UserSite> list = dao.GetModelList(String.Format("uid = '{0}' and sns_id = '{1}'", uid, sid));

            if (list.Count == 0)
                return null;

            return list[0];
        }

        public void getAccessToken(int sns, string code, int user_id)
        {
            //获得access token
            sina.GetAccessTokenByAuthorizationCode(code);

            //未绑定
            if (this.isSaveUserSite(sns, user_id) == null)
                this.saveUserSite(sns, user_id);
        }

        public bool isConnect(int sid)
        {
            SNSAPI.ISnsSerive serive = this.getSnsService(sid);
            return serive.isConnect();


        }


        public UnlessComment.Model.UserSite isSaveUserSite(int sid, int user_id)
        {
            SNSAPI.ISnsSerive serive = this.getSnsService(sid);
            UnlessComment.BLL.UserSite dao = new UnlessComment.BLL.UserSite();
            List<UnlessComment.Model.UserSite> list = dao.GetModelList(String.Format("user_id = '{0}' and sns_id = '{1}'", user_id, sid));

            if (list.Count == 0)
                return null;
           
            return list[0];
        }

    }
}